// **********************************************//
// 全局js
// 
// @author 张卫东
// @date 2014-1-10
// **********************************************//

var gui = require("nw.gui");
var win = gui.Window.get();
var os = require("os");
var fs = require("fs");

var jbody = $("body");
var panel = $("#panel");

function resizeAll() {

    var sw = window.screen.width;
    var sh = window.screen.height;

    var fw = sw * 0.6;
    var fh = sh * 0.6;

    var fx = (sw - fw) / 2;
    var fy = (sh - fh) / 2;
    log(fx + ":" + fy);

    win.resizeTo(fw, fh);
    win.moveTo(fx, fy);

}
/**
 * 重置指定元素高度，用parentId减去其他id
 * 
 * @param {}
 *            divId
 * @param {}
 *            parentId
 * @param {}
 *            otherId
 */
function resizeDiv(divId, parentId, otherId) {
    if (arguments.length < 2) {
        throw "参数异常，至少为2个";
    }
    var others = [];
    for (var i = 2; i < arguments.length; i++) {
        others.push(arguments[i]);
    }
    var ph = parseInt($("#" + parentId).height());
    var oh = 0;
    for (var i = 0; i < others.length; i++) {
        oh += parseInt($("#" + others[i]).height());
    }
    $("#" + divId).height(ph - oh).css("overflow", "auto");
}

function log(obj) {
    console.log(obj);
}

function loadPage(path) {
    loadAll();
    $("body").load(path, function() {
                unLoadAll();
            });
}

function goPage(path) {
    loadAll();
    window.location.href = path;
}

function loadAll() {
    $("body").showLoading();
}

function unLoadAll() {
    $("body").hideLoading();
}

function addMouseColor(obj, onColor, outColor) {
    if (!outColor) {
        outColor = $(obj).css("background-color");
    }
    $(obj).mouseover(function() {
                $(this).css("background-color", onColor);
            }).mouseout(function() {
                $(this).css("background-color", outColor);
            });
}

function addMouseCss(obj, onCss, outCss) {
    $(obj).mouseover(function() {
                $(this).css(onCss);
            }).mouseout(function() {
                $(this).css(outCss);
            });
}

function addMouseClass(obj, onCss) {
    $(obj).mouseover(function() {
                $(this).addClass(onCss);
            }).mouseout(function() {
                $(this).removeClass(onCss);
            });
}

function dirChooser(ele, callback) {
    var realChoose = $("#fileDialog");
    if (realChoose.length == 0) {
        var realChoose = $('<input style="display:none;" id="fileDialog" type="file" nwdirectory  />')
                .appendTo($("body"));
    }
    realChoose.change(function(evt) {
                var path = $(this).val()
                path = path.replace(/\//g, "\\\\")
                path = path.replace(/\\/g, "/")
                callback(path);
            });
    $(ele).click(function() {
                realChoose.click();
            });
}
